package model.fitnessCalculationStrategies;

import model.organism.Organism;
import model.organism.SimpleStringOrganism;

public class StringParityFitnessCalculationStrategy implements FitnessCalculationStrategy {
	
	private static Organism idealOrganism;

	@Override
	public float calculateFitness(Organism organism) throws NullPointerException
	{
		if(idealOrganism == null)
		{
			throw new NullPointerException("Ideal organism has not been set");
		}
		
		int numberOfMatchingChromosones = compareOrganismChromosones((SimpleStringOrganism)idealOrganism, (SimpleStringOrganism)organism);
		return (float)numberOfMatchingChromosones/SimpleStringOrganism.getNumberOfChromosones();
	}

	private int compareOrganismChromosones(SimpleStringOrganism idealOrganism, SimpleStringOrganism organism) 
	{
		int matchingChromosones = 0;
		for(int i = 0; i < idealOrganism.getChromosones().size(); i++)
		{
			if(idealOrganism.getChromosoneAtIndex(i).equals(organism.getChromosoneAtIndex(i)))
			{
				matchingChromosones++;
			}
		}
		return matchingChromosones;
	}

	public void setIdealOrganism(Organism ideal) {
		idealOrganism = ideal;
	}

}
